<?php
namespace yakexi;
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Request
{
	public $method = 'get';
	public $body;
	public $remote_addr = '0.0.0.0';
	public $url;
	public $path;
	public $query_string;
	public $headers;
	public $cookies;

	public function __construct()
	{
		$this->method = strtolower($_SERVER['REQUEST_METHOD']);

		$this->body = self::sanitize(file_get_contents('php://input'));

		if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
		{
			// Use the forwarded IP address, typically set when the
			// client is using a proxy server.
			$this->remote_addr = $_SERVER['HTTP_X_FORWARDED_FOR'];
		}
		elseif (isset($_SERVER['HTTP_CLIENT_IP']))
		{
			// Use the forwarded IP address, typically set when the
			// client is using a proxy server.
			$this->remote_addr = $_SERVER['HTTP_CLIENT_IP'];
		}
		elseif (isset($_SERVER['REMOTE_ADDR']))
		{
			// The remote IP address
			$this->remote_addr = $_SERVER['REMOTE_ADDR'];
		}
		if (strpos($this->remote_addr, ",") !== False)
		{
			$tmp = explode(',', $this->remote_addr);
			$this->remote_addr = $tmp[0];
		}

		if (!is_https())
			$this->url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
		else
			$this->url = 'https://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];

		$this->path = $_SERVER['SCRIPT_URL'];

		$this->query_string = $_SERVER['QUERY_STRING'];

		$this->headers = getallheaders();

		$this->cookies = $_COOKIE;

		// Sanitize all request variables
		$_GET    = self::sanitize($_GET);
		$_POST   = self::sanitize($_POST);
		$_COOKIE = self::sanitize($_COOKIE);

		if ($this->method !== 'get' && $this->method !== 'post')
		{
			$global_name = '_' . $_SERVER['REQUEST_METHOD'];
			$GLOBALS[$global_name] = array();
			if ($this->body !== '')
				parse_str($this->body, $GLOBALS[$global_name]);
		}
	}

	public function get($argument_name, $default_value='')
	{
		$global_array_name = '_' . $_SERVER['REQUEST_METHOD'];
		return $this->fetch_from_array($GLOBALS[$global_array_name], $argument_name, $default_value);
	}

	public function get_all($argument_name)
	{
	}

	public function arguments()
	{
		$global_name = '_' . $_SERVER['REQUEST_METHOD'];
		return array_keys($GLOBALS[$global_name]);
	}

	public function get_range($name, $min_value=null, $max_value=null, $default=0)
	{
		$value = intval($this->get($name, $default));
		if (!is_null($max_value))
			$value = min($value, $max_value);
		if (!is_null($min_value))
			$value = max($value, $min_value);
		return $value;
	}

	private function fetch_from_array($array, $argument_name, $default_value)
	{
		if (isset($array[$argument_name]))
			return $array[$argument_name];
		return $default_value;
	}

	/**
	 * Recursively sanitizes an input variable:
	 *
	 * - Strips slashes if magic quotes are enabled
	 * - Normalizes all newlines to LF
	 *
	 * @param   mixed  any variable
	 * @return  mixed  sanitized variable
	 */
	public static function sanitize($value)
	{
		if (is_array($value) OR is_object($value))
		{
			foreach ($value as $key => $val)
			{
				// Recursively clean each value
				$value[$key] = self::sanitize($val);
			}
		}
		elseif (is_string($value))
		{
			@$value = iconv("UTF-8", "UTF-8", $value);

			if (get_magic_quotes_gpc())
			{
				// Remove slashes added by magic quotes
				$value = stripslashes($value);
			}

			if (strpos($value, "\r") !== False)
			{
				// Standardize newlines
				$value = str_replace(array("\r\n", "\r"), "\n", $value);
			}
		}

		return $value;
	}
}